// start雷达图初始化数据
var tms, tps, bjtps, xxjss, nums, xpzcss, zdys, csm, csrs; //分享时的参数
var app = getApp(); //获取用户信息
var sys = wx.getSystemInfoSync();
var windowWidth = sys.windowWidth;
var mW = 340;
var mH = 640;
var mdData = null; //绘制的数据
var mData = [];
var mCount = null; //边数
var bqs = null;
var mCenter = ''; //中心点
var mRadius = ''; //半径(减去的值用于给绘制的文本留空间)
var mAngle = null; //角度
var mCtx = null;
var mColorPolygon = '#000000'; //多边形颜色
var mColorLines = '#000000'; //伞骨颜色
var mColorText = '#000000'; //文字颜色
var Interval = '';
var lineInterval = '';
var totalTime = 1000; //总执行时间
var spaceTime = 10; //每隔多久执行一次
var speed = spaceTime / totalTime; //每执行一次完成的进度百分比
var precent = 0; //当前完成的进度百分比
var precent2 = 0;
var lineprecent = 0;
var ctx1 = wx.createCanvasContext('radarcanvas1');
var ctx2 = wx.createCanvasContext('radarcanvas2');
var ctx3 = wx.createCanvasContext('radarcanvas3');
var ctx4 = wx.createCanvasContext('radarcanvas4');
var ctx5 = wx.createCanvasContext('radarcanvas5');
var allSpaceTime = 50; //线程执行间隔时间
var tjcs = new Array();
var jtggzt = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canvasW: mW * 2,
    canvasH: mH * 1.5,
    hidden: !0,
    hidden1: !1,
    tms: '我的形象雷达图',
    ztpj: null,
    yhnc: null,
    yhtx: null,
    csxx: null,
    xcxewm: '/images/xcxm.png',
    csnr: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (m) {
    jtggzt = 0;
    //跳转页面参数
    tms = m.tms, tps = m.tps, bjtps = m.bjtps, xxjss = m.xxjss, nums = m.nums, xpzcss = m.xpzcss, zdys = m.zdys, csm = m.csm, csrs = m.csrs;
    if (m.bq5 == 'null') {
      mdData = [
        [m.bq1, m.pf1],
        [m.bq2, m.pf2],
        [m.bq3, m.pf3],
        [m.bq4, m.pf4]
      ];
      bqs = 2;
    } else if (m.bq6 == 'null') {
      mdData = [
        [m.bq1, m.pf1],
        [m.bq2, m.pf2],
        [m.bq3, m.pf3],
        [m.bq4, m.pf4],
        [m.bq5, m.pf5]
      ];
      bqs = 3;
    } else if (m.bq7 == 'null') {
      mdData = [
        [m.bq1, m.pf1],
        [m.bq2, m.pf2],
        [m.bq3, m.pf3],
        [m.bq4, m.pf4],
        [m.bq5, m.pf5],
        [m.bq6, m.pf6]
      ];
      bqs = 4;
    } else if (m.bq8 == 'null') {
      mdData = [
        [m.bq1, m.pf1],
        [m.bq2, m.pf2],
        [m.bq3, m.pf3],
        [m.bq4, m.pf4],
        [m.bq5, m.pf5],
        [m.bq6, m.pf6],
        [m.bq7, m.pf7]
      ];
      bqs = 5;
    } else {
      mdData = [
        [m.bq1, m.pf1],
        [m.bq2, m.pf2],
        [m.bq3, m.pf3],
        [m.bq4, m.pf4],
        [m.bq5, m.pf5],
        [m.bq6, m.pf6],
        [m.bq7, m.pf7],
        [m.bq8, m.pf8]
      ];
      bqs = 6;
    }
    this.setData({
      tms: m.csm,
      ztpj: m.ztpj,
      yhtx: app.globalData.userInfo.avatarUrl,
      yhnc: app.globalData.userInfo.nickName,
      csxx: mdData
    });
    //数据信息
    mCount = mdData.length;
    mAngle = Math.PI * 2 / mCount;
    var m = [];
    for (var i = 0; i < mCount; i++) {
      m[(i + bqs) % mCount] = mdData[i];
    }
    mData = m; //内在
    mCenter = this.rpx(mW); //中心点
    mRadius = mCenter - this.rpx(100); //半径(减去的值用于给绘制的文本留空间)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var o = this;
    if (app.globalData.csnr == null) {
      wx.request({
        url: app.globalData.url + "ndzscs/request1/main.php",
        method: "GET",
        header: {
          "content-type": "application/json"
        },
        success: function (c) {
          for (var i = 0; i < 15; i++) {
            tjcs[i] = c.data[i];
          }
          o.setData({
            csnr: tjcs
          })
        }
      })//获取推荐测试
    } else {
      for (var i = 0; i < 15; i++) {
        tjcs[i] = app.globalData.csnr[i];
      }
      o.setData({
        csnr: tjcs
      })
    }//提前加载成功
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.radarcanvas1(); //雷达图文字+边框
    that.clickCanvas(); //调用画雷达图
  },
  // 单位转换
  rpx: function (param) {

    if (windowWidth == 0) {
      wx.getSystemInfo({
        success: function (res) {
          windowWidth = res.windowWidth;
        }
      });
    }
    return Number((windowWidth / 750 * param).toFixed(2));
  },
  // 绘制多边伞架
  drawPolygon: function (ctx) {
    ctx.save();
    ctx.setStrokeStyle(mColorPolygon);
    var r = mRadius / mCount; //单位半径
    //画8个圈
    for (var i = 0; i < mCount; i++) {
      ctx.beginPath();
      var currR = r * (i + 1); //当前半径
      //画8条边
      for (var j = 0; j < mCount; j++) {
        var x = mCenter + currR * Math.cos(mAngle * j);
        var y = mCenter + currR * Math.sin(mAngle * j);

        ctx.lineTo(x, y);
      }
      ctx.closePath()
      ctx.stroke();
    }
  },

  //绘制伞骨
  drawLines: function (ctx) {
    ctx.save();
    ctx.beginPath();
    ctx.setStrokeStyle(mColorLines);
    for (var i = 0; i < mCount; i++) {
      var x = mCenter + mRadius * Math.cos(mAngle * i) * lineprecent;
      var y = mCenter + mRadius * Math.sin(mAngle * i) * lineprecent;
      ctx.moveTo(mCenter, mCenter);
      ctx.lineTo(x, y);
    }
    ctx.stroke();
    var oldPrectet = lineprecent;
    lineprecent = lineprecent + speed;
    if (oldPrectet < 1 && lineprecent > 1) {
      lineprecent = 1;
    }
  },
  //绘制顶点文本
  drawText: function (ctx) {
    ctx.save();
    var fontSize = mCenter / 12 * 1;
    // ctx.font = fontSize + 'px Microsoft Yahei';
    ctx.setFontSize(fontSize);
    ctx.setFillStyle(mColorText);
    for (var i = 0; i < mCount; i++) {
      var x = mCenter + mRadius * Math.cos(mAngle * i);
      var y = mCenter + mRadius * Math.sin(mAngle * i);
      //通过不同的位置，调整文本的显示位置
      if (mAngle * i >= 0 && mAngle * i < Math.PI / 2) {
        ctx.fillText(mData[i][0], x + 15, y + fontSize - 5);
      } else if (mAngle * i == Math.PI / 2) {
        ctx.fillText(mData[i][0], x - 10, y + fontSize + 10);
      } else if (mAngle * i > Math.PI / 2 && mAngle * i <= Math.PI) {
        ctx.fillText(mData[i][0], x - 40, y + fontSize - 10);
      } else if (mAngle * i > Math.PI && mAngle * i <= Math.PI * 3 / 2) {
        ctx.fillText(mData[i][0], x - 15, y - 10);
      } else {
        ctx.fillText(mData[i][0], x, y - 8);
      }
    }
  },
  circleImg: function (ctx, img, x, y, r) {
    ctx.save();
    var d = 2 * r;
    var cx = x + r;
    var cy = y + r;
    ctx.beginPath();
    ctx.arc(cx, cy, r, 0, 2 * Math.PI);
    ctx.clip();
    ctx.drawImage(img, x, y, d, d);
    console.log("ok")
  },
  //绘制数据区域
  drawRegion: function (ctx, outColor) {
    ctx.save();
    ctx.beginPath();
    for (var i = 0; i < mCount; i++) {
      var x = mCenter + mRadius * Math.cos(mAngle * i) * mData[i][1] / 100 * precent;
      var y = mCenter + mRadius * Math.sin(mAngle * i) * mData[i][1] / 100 * precent;
      ctx.lineTo(x, y);
    }
    ctx.closePath();
    ctx.save();
    ctx.setFillStyle(outColor);
    ctx.fill();
    ctx.draw(0, 0, 500, 500);
    var oldPrectet = precent;
    precent = precent + speed;
    if (oldPrectet < 1 && precent > 1) {
      precent = 1;
    }
  },


  //画雷达
  clickCanvas: function () {
    var that = this;
    precent = 0;
    precent2 = 0;
    lineprecent = 0;
    clearInterval(that.Interval);
    clearInterval(that.lineInterval);
    ctx1.clearRect(0, 0, mW, mH);
    ctx2.clearRect(0, 0, mW, mH);
    ctx3.clearRect(0, 0, mW, mH);
    ctx4.clearRect(0, 0, mW, mH);
    ctx5.clearRect(0, 0, mW, mH);
    var outColor = "rgba(0,129,255,0.8)";
    that.lineInterval = setInterval(function () { //渐进画伞骨
      if (lineprecent <= 1) {
        that.drawLines(ctx2);
        ctx2.draw(0, 0, 500, 500);
      } else {
        clearInterval(that.lineInterval);
      }
    }, spaceTime - 20);
    setTimeout(function () { //渐进画数据区域块
      that.Interval = setInterval(function () {
        if (precent <= 1) {
          that.drawRegion(ctx3, outColor);
        } else {
          clearInterval(that.Interval);
        }
      }, spaceTime - 20);
    }, 10);
  },
  radarcanvas1: function () {
    var that = this;
    const ctx1 = wx.createCanvasContext('radarcanvas1');
    precent = 0;
    lineprecent = 0;
    that.drawPolygon(ctx1); //画伞架
    that.drawText(ctx1); //画文字
    ctx1.draw(0, 0, 500, 500);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "我完成了“" + tms + "”你也来评测一下吧！",
      path: "/pages/main/main?tm=" + tms + "&tp=" + tps + "&bjtp=" + bjtps + "&xxjs=" + xxjss + "&num=" + nums + "&xpzcs=" + xpzcss + "&zdy=" + zdys + "&csrs=" + csrs,
    };
  },
  share: function () { //获取测试内容
    jtggzt = 0;
    var e = this;
    e.setData({
      hidden: !1,
      hidden1: !0
    })
    e.clickCanvas();
  },
  kscss: function (t) {
    var o = t.currentTarget.dataset.index;
    wx.redirectTo({
      url: "/pages/ceshi/ceshi?tm=" + this.data.csnr[o][1] + "&tp=" + this.data.csnr[o][4] + "&bjtp=" + this.data.csnr[o][5] + "&xxjs=" + this.data.csnr[o][3] + "&num=" + this.data.csnr[o][0] + "&xpzcs=0" + "&zdy=" + this.data.csnr[o][10] + "&csrs=" + this.data.csnr[o][6],
    });
  },
})